package controller

import (
	"iot-bridge/iot_cmd_up/distribute/service"
)

const (
	TcpReport = 2
)

var (
	TcpReportService *service.TcpReportService
)

func Init() error {
	TcpReportService = service.NewTcpReportService()
	return nil
}

func Close() {
}

//func packetLen(packet []byte) (uint, uint) {
//	len := uint(0)
//	count := uint(0)
//	for _, v := range packet {
//		count++
//		len = len << 7
//		len = len | uint(v&^(1<<7))
//		if v&(1<<7) == 0 {
//			break
//		}
//	}
//	return len, count
//}

func packetLen(packet []byte) (uint, uint) {
	flag128 := 128 // 1000 0000
	value := 0
	var shortIdLenData []byte
	var multiplier = 1
	index := 0
	for index < len(packet) {
		item := packet[index]
		shortIdLenData = append(shortIdLenData, item)
		index = index + 1
		if item&byte(flag128) == 0 {
			break
		}
	}
	for _, v := range shortIdLenData {
		digit := int(v)
		value = value + (digit&127)*multiplier
		multiplier = multiplier << 7
		if (digit & 128) != 0 {
			continue
		}
	}
	return uint(value), uint(index)
}
